package cn.tiancaifan.micro.controller.system.basic;


import cn.tiancaifan.micro.entity.Role;
import cn.tiancaifan.micro.entity.SystemResults;
import cn.tiancaifan.micro.service.MenuService;
import cn.tiancaifan.micro.service.PermissService;
import cn.tiancaifan.micro.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/basic/per")
public class PermissController {

    @Autowired
    RoleService roleService;

    @Autowired
    PermissService permissService;

    @Autowired
    MenuService menuService;


    @GetMapping("/")
    public SystemResults getAllRoles(){
        SystemResults ok = SystemResults.ok(null, permissService.getAllMenus());
        return ok;
    }

    @GetMapping("/mids/{id}")
    public SystemResults getAllRoles(@PathVariable Integer id){
        SystemResults ok = SystemResults.ok(null, permissService.getMidsByRid(id));
        return ok;
    }

    @PutMapping("/")
    public SystemResults updateMenuRole(Integer rid, Integer[] mids) {
        if (menuService.updateMenuRole(rid, mids)) {
            return SystemResults.ok("更新成功!");
        }
        return SystemResults.error("更新失败!");
    }

    @DeleteMapping("/role/{rid}")
    public SystemResults deleteRoleById(@PathVariable Integer rid) {
        if (roleService.deleteRoleById(rid) == 1) {
            return SystemResults.ok("删除成功!");
        }
        return SystemResults.error("删除失败!");
    }

    @PostMapping("/role")
    public SystemResults addRole(@RequestBody Role role) {
        if (roleService.addRole(role) == 1) {
            return SystemResults.ok("添加成功!");
        }
        return SystemResults.error("添加失败!");
    }

}
